#include <16F877A.h>

/*========================================== Controller Configuration Settings========================================*/
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

/*============================================= Global Variables======================================================*/
unsigned char sec,min,hour,sec1,min1,hour1,day,date,month,year;

/*===============================================Pin Definitions======================================================*/
#define SELECT 0
#define UP PIN_C1
#define DOWN PIN_C0
#define EXIT PIN_C2
#define ENTER PIN_C3


#define rel1 PIN_B0
#define rel2 PIN_B1
#define rel3 PIN_B7

/* ==============================================Inclusion files======================================================*/

#include "rtc.h"
#include "LCD_INIT.H"


/*======================================================main function=================================================*/
void main()
{
   unsigned char g;
   int temp;
   int h,m,s,h_conv,m_conv,s_conv,temp_adc;
   port_b_pullups(true);
   ext_int_edge(0,H_TO_L);
   set_tris_b(0xfe);
   buzzer(5,50);
   delay_ms(10);
   setup_port_a(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);
   delay_ms(100);

   Lcd_init();
   delay_ms(100);
/*=================================================screen 1===========================================================*/
   row1();
   display("  DISTRIBUTION");
   row2();
   display("   AUTOMATION");
   delay_ms(100);
   RTC_Init();          // initial DS1307
   sec=RTC_Read(0);
   RTC_Write(0,sec & 0x7F); // enable oscillator(bit 7 =0)
   RTC_RD();
   temp = sec;
   delay_ms(2000);
   RTC_RD();
   delay_ms(100);
   if(temp==sec)
   {
      row2();
      display("Error in RTC");
      delay_ms(15000);
   }
   else
   {


      while(1)
      {

                  if((input(ENTER) == SELECT) && (input(UP) == SELECT))
            {
                  LCD_Clear();
                  display("Enter Hour ");
                  h=timemode_call(1);
                  h_conv=dec_to_hex(h);
                  delay_ms(1000);
                  row1();
                  display("Enter Minute");
                  m=timemode_call(2);
                  m_conv=dec_to_hex(m);
                  delay_ms(1000);
                  row1();
                  display("Enter Second");
                  s=timemode_call(3);
                  s_conv=dec_to_hex(s);
                  DS1307_settime(h_conv,m_conv,s_conv);
                  buzzer(3,500);
                  LCD_Clear();
             }


         RTC_RD();
            if(temp!=sec)
            {
               row1();
               display("TIME:  ");
               LCD_RTC(hour);
               display(":");
               LCD_RTC(min);
               display(":");
               LCD_RTC(sec);
               temp=sec;
            }

                  delay_ms(100);
            temp_adc=read_adc();
                  delay_ms(100);

           if(temp_adc>215)
           {
           row2();
           display("LOAD A B C ON");
           output_high(rel1);
           output_high(rel2);
           output_high(rel3);
                  delay_ms(2000);
           }
           else if(temp_adc>125 && temp_adc<215)
           {
              if(hour>6 && hour<8)
              {
               row2();
               display("LOAD B C ON   ");
              output_low(rel1);
              output_high(rel2);
              output_high(rel3);
              }
              else if(hour>8 && hour<10)
              {
           row2();
           display("LOAD A C ON    ");
              output_high(rel1);
              output_low(rel2);
              output_high(rel3);
              }
              else
              {
           row2();
           display("LOAD A B ON    ");
              output_high(rel1);
              output_high(rel2);
              output_low(rel3);
              }
                  delay_ms(2000);
           }
           else
           {
              if(hour>6 && hour<8)
              {
           row2();
           display("LOAD C ON      ");
              output_low(rel1);
              output_low(rel2);
              output_high(rel3);
              }
              else if(hour>8 && hour<10)
              {
           row2();
           display("LOAD A ON      ");
              output_high(rel1);
              output_low(rel2);
              output_low(rel3);
              }
              else
              {
           row2();
           display("LOAD B ON      ");
              output_low(rel1);
              output_high(rel2);
              output_low(rel3);
              }
                  delay_ms(2000);
           }
        }
   }
}

